package com.gsit.orc.util;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.media.CannotRealizeException;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.RestartingEvent;
import javax.media.StartEvent;
import javax.media.StopByRequestEvent;
import javax.media.Time;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.openqa.jetty.html.Image;

public class MediaPanel extends JPanel implements ControllerListener{
	
	
	private Player mediaPlayer = null;
	private URL mediaURL = null; 
	private MediaPlayerUtil mediaPlayerUtil;
	
	private JButton button = new JButton("Message Reseciever");
	
	private static Boolean playerReady = Boolean.TRUE;
	
	public void start()throws Exception{
		mediaPlayer.start();
	}
	
	
	private void rewind() {
		mediaPlayer.setMediaTime(new Time(0));
	}
	
	private void createMedia() {
		setLayout(new BorderLayout()); 
		

		Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, true);

		try {
			mediaPlayer = Manager.createRealizedPlayer(mediaURL);

			Component video = mediaPlayer.getVisualComponent();
			Component controls = mediaPlayer.getControlPanelComponent();
			
			mediaPlayer.addControllerListener(this);
			this.add(new JLabel(ResourceLoader.getImage(ResourceLoader.getURL("logo.png"))), BorderLayout.NORTH); 

			if (video != null)
				this.add(video, BorderLayout.CENTER); 

			if (controls != null)
				this.add(controls, BorderLayout.SOUTH); 
			
			
			//this.add(button, BorderLayout.EAST); 
			
			button.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent actionEvent) {
					 
					System.out.println("Message reseived .....");
					setPlayerReady(Boolean.TRUE);
					
				}
			});
			
			mediaPlayer.prefetch();
			
			
		}catch (NoPlayerException noPlayerException) {
			System.err.println("No media player found");
		}catch (CannotRealizeException cannotRealizeException) {
			System.err.println("Could not realize media player");
		}catch (IOException iOException) {
			System.err.println("Error reading from the source");
		}catch (Exception iOException) {
			System.err.println("Error reading from the source");
		}
	}
	
	public MediaPanel(URL mediaURL,MediaPlayerUtil mediaPlayerUtil) {
		this.mediaURL = mediaURL;
		this.mediaPlayerUtil =mediaPlayerUtil;
		createMedia();
	}

	@Override
	public void controllerUpdate(ControllerEvent event) {
		if (event instanceof EndOfMediaEvent) {
			rewind();
		} else if(event instanceof StopByRequestEvent){
			playerReady = Boolean.TRUE;
			//mediaPlayer.stop();
		}else if(event instanceof StartEvent){
			playerReady = Boolean.FALSE;
			//mediaPlayer.start();
		}else if(event instanceof RestartingEvent){
			playerReady = Boolean.FALSE;
			//mediaPlayer.start();
		}
		
	} 
	
	public Boolean isPlayerReady(){
		return playerReady;
	}
	
	public void setPlayerReady(Boolean state){
		 playerReady = state;
	}
} 
